package com.gemme.gemme.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.gwc.PaySucessActivity;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import org.json.JSONObject;

import java.util.HashMap;

public class ShareDialog {

    boolean isPerson = true;
    boolean isDetail = true;
    private Context mContext;
    private String imagestr;
    private String url;
    private String title;
    private String desc;

    public void showDialog(Context mContext){
        this.mContext = mContext;
        //new int[]{R.id.close}
        BottomDialog dialog = new BottomDialog(mContext, R.layout.share_dialog,
                new int[]{R.id.close,R.id.iv_weixin,R.id.iv_weixin_pyq});

//        initView(dialog.getView());
        dialog.show();
        dialog.setOnBottomItemClickListener(new BottomDialog.OnBottomItemClickListener() {
            @Override
            public void onBottomItemClick(BottomDialog dialog, View view) {
                switch (view.getId()) {
                    case R.id.close:
                        dialog.dismiss();
                        break;

                    case R.id.iv_weixin:
                        sharesWeixin(SHARE_MEDIA.WEIXIN); //调用微信分享
                        break;
                    case R.id.iv_weixin_pyq:
                         sharesWeixin(SHARE_MEDIA.WEIXIN_CIRCLE); //调用微信朋友圈分享
                         break;



                }
            }
        });
    }


    private void sharesWeixin(SHARE_MEDIA type) {
        Activity act = null;
        if(mContext instanceof Activity)
            act = (Activity)mContext;

        if(act == null)
            return;
        UMImage image = new UMImage(mContext, imagestr);//网络图片
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setThumb(image);  //缩略图
        web.setDescription(desc);//描述
//        userShareSaveData("WEChAT","https://new.qq.com/omn/20200709/20200709A05NXP00.html");
        userShareSaveData("WEChAT",url);

        new ShareAction(act)
                .setPlatform(type)//传入平台 SHARE_MEDIA.WEIXIN
                .withText("hello")//分享内容
                .withMedia(web)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        System.out.println("分享 : onStart");
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        System.out.println("分享 : onResult");
                    }

                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        System.out.println("分享 : onError");
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        System.out.println("分享 : onCancel");
                    }
                })//回调监听器
                .share();

    }

    private void userShareSaveData(String type, String h5_url) {
        SharedPreferences MSP = mContext.getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();
        if(TextUtils.isEmpty(type) || TextUtils.isEmpty(h5_url)|| TextUtils.isEmpty(access_token))
            return;
        String url = DataManager.BaseUrl + "user/share/goshare?";
        url = url +
                "createTime="+
                "&delFlag="+
                "&id="+
                "&sharedUserId="+
                "&status="+
                "&type="+type+
                "&updateTime="+
                "&url="+h5_url+
                "&userId="

        ;
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jb = new JSONObject(response);
                    int data = jb.getInt("data");
                    System.out.println("");
                    userShareSaveData2(data);
                }catch (Exception e){

                }
            }
        });
    }

    private void userShareSaveData2(int data) {
        SharedPreferences MSP = mContext.getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();
        if(TextUtils.isEmpty(access_token))
            return;
        String url = DataManager.BaseUrl + "user/share/sharesuccess?shareId=" + data;

        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jb = new JSONObject(response);
                    int data = jb.getInt("code");
//                    if(data == 0)
//                        Toast.makeText(TestUMengActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                    System.out.println("");
                }catch (Exception e){

                }
            }
        });
    }


    public void setData(String imagestr,String url,String title,String desc) {

        this.imagestr = imagestr;
        this.url = url;
        this.title = title;
        this.desc = desc;
    }
}
